package com.example.demo.business.service;

import com.example.demo.base.util.JsonUtil;
import com.example.demo.business.bean.base.ProxyRebase;
import com.example.demo.business.bean.entity.HProxyAward;
import com.example.demo.business.bean.entity.HUser;
import com.example.demo.business.bean.enums.ConfKey;
import com.example.demo.business.dao.HConfigDao;
import com.example.demo.business.dao.HProxyAwardDao;
import com.example.demo.business.dao.HUserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Service
public class ProxyService {
    @Autowired
    private HUserDao hUserDao;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private HConfigDao hConfigDao;

    @Autowired
    private HProxyAwardDao hProxyAwardDao;

    @Autowired
    private HUserDao userDao;

    public void calProxyReward(HUser hUser, int productId, int payMoney){
        int uid = hUser.getId();
        //计算4级代理的奖励
        for (int i = 1; i < 5; i++) {
            int referId = hUser.getReferId();
            if (referId==0) break; //没有上级
            Optional<HUser> optionalHUser= userDao.findById(referId);
            if (!optionalHUser.isPresent()) break;
            hUser = optionalHUser.get();
            //用户业绩
            int performance = (payMoney*getProxyRadio(i))/100;
            //用户收入
            int radio = getProxyRebaseRadio(hUser.getAllPerformance());
            int userIncome = (performance*radio)/100;
            //保存用户信息
            hUser.setAllPerformance(hUser.getAllPerformance()+performance);
            hUser.setBalance(hUser.getBalance().add(new BigDecimal(userIncome)));
            tokenService.updateUserInfo(hUser);

            HProxyAward hProxyAward = new HProxyAward(referId,uid,i,new BigDecimal(payMoney),radio,new BigDecimal(userIncome),
                    productId,System.currentTimeMillis()/1000,System.currentTimeMillis()/1000);
            hProxyAwardDao.save(hProxyAward);
        }
    }

    //获取代理分层比例
    private int getProxyRadio(int num){
        String proxyRadio = hConfigDao.findByKeyName(ConfKey.proxyRatio.name).getKeyValue();
        ArrayList<LinkedHashMap<String,Integer>> srt1 = JsonUtil.getObjByJsonString(proxyRadio, ArrayList.class);

        Map<Integer,Integer> map = new HashMap<>();

        for (LinkedHashMap<String,Integer> item:srt1){
            item.forEach((k,v)->{
                map.put(Integer.parseInt(k),v);
            });
        }
        return map.get(num);
    }

    //获取各级代理的业绩
    public Object getProxyIncome(String token){
        int pid = tokenService.getUidByToken(token);
        List<HProxyAward> list = hProxyAwardDao.findByPid(pid);
        Map<Integer,Integer> map = new HashMap<>();
        for (int i = 1; i < 5; i++) {
            map.put(i,0);
        }
        list.forEach(item->{
            map.put(item.getPGrade(),map.get(item.getPGrade())+item.getReward().intValue());
        });
        return map;
    }
    //获取代理返利比例
    private int getProxyRebaseRadio(int userPerformance){
        String proxyRebate = hConfigDao.findByKeyName(ConfKey.proxyRebate.name).getKeyValue();
        ArrayList<LinkedHashMap<String,Integer>> srt = JsonUtil.getObjByJsonString(proxyRebate, ArrayList.class);
        List<ProxyRebase> list = new ArrayList<>();
        for (LinkedHashMap<String,Integer> item:srt){
            item.forEach((k,v)->{
                int low = Integer.parseInt(k.split("-")[0]);
                int up = Integer.parseInt(k.split("-")[1]);
                if (up ==0){
                    up = 100000000;
                }
                ProxyRebase proxyRebase = new ProxyRebase(up,low,v);
                list.add(proxyRebase);
            });
        }

        //查看业绩对应比例
        for (ProxyRebase proxyRebase:list){
            if (proxyRebase.low<userPerformance && proxyRebase.up>userPerformance){
                return proxyRebase.radio;
            }
        }
        return 0;
    }
    //获取用户返利比例
    private int getUserReturn(HUser hUser){

        return 0;
    }
}
